package org.ourgrid.broker.business.scheduler.workqueue.xmlcreator;

import java.util.Map.Entry;

import org.ourgrid.broker.status.GridProcessStatusInfo;
import org.ourgrid.broker.status.GridProcessStatusInfoResult;
import org.ourgrid.broker.status.JobStatusInfo;
import org.ourgrid.broker.status.TaskStatusInfo;

public class HereIsJobStatsXMLCreator implements XMLCreatorIF {
	
	
	/**
	 * <HERE_IS_JOB_STATS jobId='int' jobState='int' creationTime='long' finalizationTime='long'>
	 * 
	 * 		1..n <TASK_STATS taskId='int' jobId='int' taskState='String' actualFails='int' 
	 * 				creationTime='long' finalizationTime='long'>
	 * 
	 * 			 1..n <GRID_PROCESS_STATS processId='int' taskId='int' jobId='int' processState='String' 
	 * 					processPhase='String' workerInfo='String' creationTime='long' finalizationTime='long'> 
	 * 						
	 * 				  		<RESULT executionError='String' executionErrorCause='String' initDataTime='long'
	 * 						 remoteDataTime='long' finalDataTime='long' exitValue='int' stderr='String' stdout='String'
	 * 						 sabotageCheck='String'/>
	 * 
	 * 				  </GRID_PROCESS_STATS>
	 * 
	 * 			 </TASK_STATS>
	 * 			
	 * 		1..n <PEER_TO_REPORT peerId='String' requestId='long'/>
	 * 
	 * </HERE_IS_JOB_STATS> 
	 * 
	 */
	public String getXML(JobStatusInfo jobStatusInfo) {
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("<HERE_IS_JOB_STATS ");
		buffer.append("jobId='" + jobStatusInfo.getJobId() + "' ");
		buffer.append("jobState='" + jobStatusInfo.getState() + "' ");
		buffer.append("creationTime='" + jobStatusInfo.getCreationTime() + "' ");
		buffer.append("finalizationTime='" + jobStatusInfo.getFinalizationTime() + "' ");
		buffer.append(">");
		
		for (TaskStatusInfo task : jobStatusInfo.getTasks()) {
		
			buffer.append("<TASK_STATS ");
			buffer.append("taskId='" + task.getTaskId() + "' ");
			buffer.append("jobId='" + task.getJobId() + "' ");
			buffer.append("taskState='" + task.getState() + "' ");
			buffer.append("actualFails='" + task.getActualFails() + "' ");
			buffer.append("creationTime='" + task.getCreationTime() + "' ");
			buffer.append("finalizationTime='" + task.getFinalizationTime() + "' ");
			buffer.append(">");
			
			for (GridProcessStatusInfo gridProcess : task.getGridProcesses()) {
				
				buffer.append("<GRID_PROCESS_STATS ");
				buffer.append("processId='" + gridProcess.getId() + "' ");
				buffer.append("taskId='" + gridProcess.getTaskId() + "' ");
				buffer.append("jobId='" + gridProcess.getJobId() + "' ");
				buffer.append("processState='" + gridProcess.getState() + "' ");
				buffer.append("processPhase='" + gridProcess.getPhase() + "' ");
				buffer.append("workerInfo='" + gridProcess.getWorkerInfo().getWorkerID() + "' ");
				buffer.append("creationTime='" + gridProcess.getCreationTime() + "' ");
				buffer.append("finalizationTime='" + gridProcess.getFinalizationTime() + "' ");
				buffer.append(">");
				
				GridProcessStatusInfoResult result = gridProcess.getReplicaResult();
				
				buffer.append("<RESULT ");
				buffer.append("executionError='" + result.getExecutionError() + "' ");
				buffer.append("executionErrorCause='" + result.getExecutionErrorCause() + "' ");
				buffer.append("initDataTime='" + result.getInitDataTime() + "' ");
				buffer.append("remoteDataTime='" + result.getRemoteDataTime() + "' ");
				buffer.append("finalDataTime='" + result.getFinalDataTime() + "' ");
				
				if (result.getExecutorResult() != null) {
					buffer.append("exitValue='" + result.getExecutorResult().getExitValue() + "' ");
				}
				buffer.append("sabotageCheck='" + (result.getSabotageCheck() == null ? "" : result.getSabotageCheck()) + "' ");
				buffer.append(">");
				
				if (result.getExecutorResult() != null) {
					
					buffer.append("<stderr> <![CDATA[" + XMLUtils.toValidXML(result.getExecutorResult().getStderr()) + "]]> </stderr>");
					buffer.append("<stdout> <![CDATA[" + XMLUtils.toValidXML(result.getExecutorResult().getStdout()) + "]]> </stdout> ");
				}
				
				
				buffer.append("</RESULT>");
				
				buffer.append("</GRID_PROCESS_STATS>");
			}
			
			buffer.append("</TASK_STATS>");
		}
		
		for (Entry<String, Long> peer : jobStatusInfo.getPeersToRequests().entrySet()) {
			buffer.append("<PEER_TO_REPORT peerId='" + peer.getKey() + "' requestId='" + peer.getValue() + "' />");
		}
		
		buffer.append("</HERE_IS_JOB_STATS>");
		
		return buffer.toString();
	}
	
}
